<?php
namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\BalanceLog;
use App\Models\Order;
use Illuminate\Http\Request;

class DashboardController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:user');
    }

    public function index(Request $request)
    {
        // 获取今日订单
        $todayOrdersTotal = Order::query()
            ->where('user_id', $request->user()->id)
            ->whereDate('paid_at', date('Y-m-d'))
            ->where('status', Order::STATUS_PAID)
            ->count();

        $todayMoneyTotal = Order::query()
            ->where('user_id', $request->user()->id)
            ->whereDate('paid_at', date('Y-m-d'))
            ->where('status', Order::STATUS_PAID)
            ->sum('pay_amount');

        $yesterdayOrdersTotal = Order::query()
            ->where('user_id', $request->user()->id)
            ->whereDate('paid_at', date('Y-m-d', strtotime('-1 day')))
            ->where('status', Order::STATUS_PAID)
            ->count();

        $yesterdayMoneyTotal = Order::query()
            ->where('user_id', $request->user()->id)
            ->whereDate('paid_at', date('Y-m-d', strtotime('-1 day')))
            ->where('status', Order::STATUS_PAID)
            ->sum('pay_amount');

        return view('user.dashboard', compact('todayMoneyTotal', 'todayOrdersTotal', 'yesterdayMoneyTotal', 'yesterdayOrdersTotal'));
    }
}
